package com.task.core.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.task.common.enums.QuestionOperationTypeEnum;
import com.task.common.enums.QuestionPromptContentEnum;
import com.task.common.info.QuestionShowInfo;
import com.task.common.redis.RedisKeyConstants;
import com.task.core.domain.DeviceFraudPlatformInfo;
import com.task.core.dto.request.DeviceRequestDTO;
import com.task.core.service.DeviceFraudPlatformService;
import com.task.core.vo.resp.DeviceFraudDetailVo;
import io.jsonwebtoken.lang.Collections;
import org.springframework.stereotype.Service;

import com.task.common.annotation.DataScope;
import com.task.common.core.redis.RedisCache;
import com.task.common.domain.info.KeyAndValue;
import com.task.common.enums.QuestionInfoTypeEnum;
import com.task.common.exception.ServiceException;
import com.task.common.info.DeviceFraudInfo;
import com.task.common.info.DeviceGroupInfo;
import com.task.common.utils.SecurityUtils;
import com.task.common.utils.StringUtils;
import com.task.common.utils.uuid.UUID;
import com.task.core.dto.request.DeviceFraudRequestDTO;
import com.task.core.dto.request.QuestionInfoRequestDTO;
import com.task.core.mapper.DeviceFraudMapper;
import com.task.core.service.DeviceFraudService;
import com.task.core.service.QuestionInfoService;

/**
 * @author 迪迦.
 * @date 2024/11/5 16:25
 */
@Service("DeviceFraudService")
public class DeviceFraudServiceImpl implements DeviceFraudService {

  @Resource
  private DeviceFraudMapper deviceFraudMapper;

  @Resource
  private RedisCache redisService;

  @Resource
  private QuestionInfoService questionInfoService;


  @Resource
  private DeviceFraudPlatformService deviceFraudPlatformService;

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public List<DeviceFraudInfo> getDeviceFraudByPage(DeviceFraudRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    List<DeviceFraudInfo> deviceFraudByPage = deviceFraudMapper.getDeviceFraudByPage(request);
    for (DeviceFraudInfo info : deviceFraudByPage) {
      List<QuestionShowInfo> questionShowInfoList = questionInfoService.getQuestionInfosByTypeAndCode(QuestionInfoTypeEnum.DEVICE_FRAUD.name(), info.getCode());
      questionShowInfoList.stream().
              forEach(f ->{
                      if( QuestionOperationTypeEnum.DELETE.getCode().equals(f.getOperationType()) &&
                                QuestionInfoTypeEnum.DEVICE_FRAUD.name().equals(f.getQuestionType()) ){
                        f.setShowContent(info.getDeviceFraudName()+","+QuestionPromptContentEnum.DEVICE_FRAUD_DELETE_PROMPT_CONTENT.getMsg());
                      }else {
                        f.setShowContent(info.getDeviceFraudName()+","+f.getShowContent());
                      }
              });
      info.setQuestionInfos(questionShowInfoList);
    }
    return deviceFraudByPage;
  }

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public List<KeyAndValue> getDeviceFraudForOption(DeviceFraudRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    return deviceFraudMapper.getDeviceFraudForOption(request);
  }

  @Override
  public void addDeviceFraud(DeviceFraudRequestDTO request) {
    if (StringUtils.isEmpty(request.getVersionCode())) {
      throw new ServiceException("版本编号不能为空");
    }
    if (questionInfoService.checkVersionCode(QuestionInfoTypeEnum.DEVICE_FRAUD, request.getDeviceFraudPlatformCode(), request.getVersionCode())) {
      throw new ServiceException("关联的上层信息已更新,重新获取数据后再提交");
    }
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    request.setCode(UUID.randomUUID().toString().replace("-", ""));
    deviceFraudMapper.addDeviceFraud(request);
    DeviceFraudPlatformInfo deviceFraudPlatformInfo = deviceFraudPlatformService.getDeviceFraudPlatformById(request.getDeviceFraudPlatformCode());
    redisService.setCacheObject(RedisKeyConstants.DEVICE_FRAUD_INFO.formatKey(request.getCode()) , new DeviceFraudInfo(request.getId(),request.getCode(), request.getDeviceFraudName(),
        request.getDeviceFraudName(), request.getConfig(), request.getDescription(),deviceFraudPlatformInfo.getDeviceFraudPlatformName()));
  }

  @Override
  public void modifyDeviceFraud(DeviceFraudRequestDTO request) {
    if (StringUtils.isEmpty(request.getVersionCode())) {
      throw new ServiceException("版本编号不能为空");
    }
    if (questionInfoService.checkVersionCode(QuestionInfoTypeEnum.DEVICE_FRAUD, request.getDeviceFraudPlatformCode(), request.getVersionCode())) {
      throw new ServiceException("关联的上层信息已更新,重新获取数据后再提交");
    }
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    Integer integer = deviceFraudMapper.modifyDeviceFraud(request);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
    DeviceFraudInfo deviceFraud = deviceFraudMapper.getDeviceFraudByCode(request);
    DeviceFraudPlatformInfo deviceFraudPlatformInfo = deviceFraudPlatformService.getDeviceFraudPlatformById(request.getDeviceFraudPlatformCode());
    redisService.setCacheObject(RedisKeyConstants.DEVICE_FRAUD_INFO.formatKey(request.getCode()) , new DeviceFraudInfo(deviceFraud.getId(),request.getCode(), request.getDeviceFraudName(),
            request.getDeviceFraudName(), request.getConfig(), request.getDescription(),deviceFraudPlatformInfo.getDeviceFraudPlatformName()));
    //查找是否存在已关联的修改过的上层数据
    List<QuestionShowInfo> questions = questionInfoService.getQuestionInfosByTypeAndCode(QuestionInfoTypeEnum.DEVICE_FRAUD.name(), request.getCode());
    if(!Collections.isEmpty(questions)){
      questions.forEach(f ->{
        String key = RedisKeyConstants.QUESTION_FIX_INFO.formatKey(request.getCode(), f.getCode());
        if(!redisService.hasKey(key)){
          redisService.setCacheObject(key,true);
        }
      });
    }
   // questionInfoService.fixQuestion(new QuestionInfoRequestDTO(QuestionInfoTypeEnum.DEVICE_FRAUD, request.getCode(), request.getOperationBy()));
  }

  @Override
  public void deleteDeviceFraud(String deviceFraudCode, Long userId) {
    Integer integer = deviceFraudMapper.deleteDeviceFraud(deviceFraudCode, userId);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
    //删除对应的关系表
    //删除任务和deviceFraud的关联
    deviceFraudMapper.deleteMissionDeviceFraudRelation(deviceFraudCode);
    redisService.deleteObject(RedisKeyConstants.DEVICE_FRAUD_INFO.formatKey(deviceFraudCode) );
  }

  @Override
  @DataScope(deptAlias = "dept",userAlias = "su")
  public DeviceFraudDetailVo getDeviceFraudDetail(String deviceFraudCode) {
    DeviceRequestDTO deviceRequestDTO = new DeviceRequestDTO();
    deviceRequestDTO.setCode(deviceFraudCode);
    return deviceFraudMapper.getDeviceFraudDetail(deviceRequestDTO);
  }
}
